/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package gestion;


import java.awt.Color;
import java.awt.event.ItemEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;
import otros.Fecha;
import otros.MiTableModel;
import otros.Util;
import tablasSql.Compras;

/**
 *
 * @author FERRETERIA
 */
public class FFacturasCompras extends javax.swing.JInternalFrame {
    
    boolean fuera;
    int filaSel;
    private final static org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(FFacturasCompras.class);
  
    public FFacturasCompras() {
        Util.setConfiguracionLog(Util.LOG4_FICH);
        initComponents();
        AnchoColumnas();
        tabla.setAutoCreateRowSorter(true);
        tabla.setColumnSelectionAllowed(false);
        
        
        String []fecha = new Fecha().getFecha().split("-");
        //txtDia.setText(fecha[0]);
        //txtMes.setText(fecha[1]);
        txtAño.setText(fecha[2]);
        
        
        txtPagar.setVisible(false);
        jLabel10.setVisible(false);
        txtSelec.setVisible(false);
        cbTipo.setSelectedIndex(3); // llama a actualizarTabla 
    }

    private void AnchoColumnas() {
        DefaultTableColumnModel colModel = (DefaultTableColumnModel)tabla.getColumnModel();       
        colModel.getColumn(0).setPreferredWidth(100);
        colModel.getColumn(1).setPreferredWidth(320);
        colModel.getColumn(2).setPreferredWidth(80);
        colModel.getColumn(3).setPreferredWidth(100);
        colModel.getColumn(4).setPreferredWidth(100);
        colModel.getColumn(5).setPreferredWidth(100);
        colModel.getColumn(6).setPreferredWidth(100);
        colModel.getColumn(7).setMaxWidth(0);       //
        colModel.getColumn(7).setMinWidth(0);       // periodo
        colModel.getColumn(7).setPreferredWidth(0); //            
        int ult = 7;
        
        if (cbTipoPago.getSelectedIndex() == 2) {   // fecha de pago
            colModel.getColumn(ult+1).setMaxWidth(0);
            colModel.getColumn(ult+1).setMinWidth(0);
            colModel.getColumn(ult+1).setPreferredWidth(0);
        } 
        else
            colModel.getColumn(ult+1).setPreferredWidth(100);
        
        switch(cbTipo.getSelectedIndex()) {
            case 0:     // factura
                break;
            case 1:     // albaran    
                //--------------------------------------------campo de seleccion
                int valor = 25;
                tabla.getColumnModel().getColumn(ult+2).setMaxWidth(valor);
                tabla.getColumnModel().getColumn(ult+2).setMinWidth(valor);
                tabla.getColumnModel().getColumn(ult+2).setPreferredWidth(valor);
                break;
            case 2:     // abono
                colModel.getColumn(ult).setMaxWidth(0);
                colModel.getColumn(ult).setMinWidth(0);
                colModel.getColumn(ult).setPreferredWidth(0);
                colModel.getColumn(ult+1).setMaxWidth(0);
                colModel.getColumn(ult+1).setMinWidth(0);
                colModel.getColumn(ult+1).setPreferredWidth(0);
                break;
        }
             
        bBorrar.setEnabled(false);
        bConvertirVenta.setEnabled(false);
        bConvertir.setEnabled(false);
    }
    
    private void ActualizarTabla() {
        
        cboxSelTodo.setVisible((cbTipo.getSelectedIndex()==1)?true:false);
        cboxSelTodo.setSelected(false);
                
        if (cbTipoPago.getSelectedIndex()!=2) {
           if (cbFecha.getItemCount() < 3) 
               cbFecha.addItem("Pago");
        }
        else
            cbFecha.removeItem("Pago");
        
        int tipo = cbTipo.getSelectedIndex();
        int id = Integer.parseInt(txtId.getText().isEmpty()? "0" :txtId.getText());
        int tipoPago = cbTipoPago.getSelectedIndex();
        int idProve = Integer.parseInt(txtIdProve.getText().isEmpty()? "-1" : txtIdProve.getText());
        int dia = Integer.parseInt(txtDia.getText().isEmpty()? "0" : txtDia.getText());
        int mes = Integer.parseInt(txtMes.getText().isEmpty()? "0" : txtMes.getText());
        int año = Integer.parseInt(txtAño.getText().isEmpty()? "0" : txtAño.getText());
        int diaH = Integer.parseInt(txtDiaH.getText().isEmpty()? "0" : txtDiaH.getText());
        int mesH = Integer.parseInt(txtMesH.getText().isEmpty()? "0" : txtMesH.getText());
        int añoH = Integer.parseInt(txtAñoH.getText().isEmpty()? "0" : txtAñoH.getText());
        
        
        int tam = tabla.getRowCount();
        for (int i = 0; i < tam; i++)
            ((DefaultTableModel)tabla.getModel()).removeRow(0); 
        
        MiTableModel datos = Compras.getTabla(tipo, id, tipoPago, idProve, 
                                              dia, mes, año, diaH, mesH, añoH,
                                                cbFecha.getSelectedIndex(), txtIdFab.getText().trim()); 
        if (cbTipo.getSelectedIndex() == 1) {   //albaran
            datos.addColumn("", new Vector<Boolean>());
            datos.setType(new Class[] {Integer.class, String.class, Float.class, String.class,
                                            String.class,String.class,String.class,
                                            String.class, Integer.class,Boolean.class});
            datos.setEditable(new Boolean[] {false,false,false,false,false,false,false,false,false,true});
        }
     
        tabla.setModel(datos);
        
        tabla.getColumnModel().getColumn(0).setCellRenderer(new tableCellRender.Id(filaSel, fuera));
        tabla.getColumnModel().getColumn(1).setCellRenderer(new tableCellRender.Texto(filaSel, fuera));
        tabla.getColumnModel().getColumn(2).setCellRenderer(new tableCellRender.Precio(filaSel, fuera));
        tabla.getColumnModel().getColumn(3).setCellRenderer(new tableCellRender.Tipo(filaSel, fuera));
        tabla.getColumnModel().getColumn(4).setCellRenderer(new tableCellRender.Fecha(filaSel, fuera));
        tabla.getColumnModel().getColumn(5).setCellRenderer(new tableCellRender.Fecha(filaSel, fuera));
        tabla.getColumnModel().getColumn(6).setCellRenderer(new tableCellRender.Fecha(filaSel, fuera)); //fechavenc
        tabla.getColumnModel().getColumn(8).setCellRenderer(new tableCellRender.Fecha(filaSel, fuera)); //fechapago
        AnchoColumnas();
        HallarTotal();
        if (cbFecha.getSelectedIndex()==1)
            HallarAPagar();
        txtNumReg.setText(""+tabla.getRowCount());
    }
    
    private void Borrar(int id) {
       Util.restaurarStock(id, false);  // borramos el stock
       Compras.borrarCompra(id);
       Compras.borrarListArticulos(id);
       
       log.info("borrar " + cbTipo.getSelectedItem().toString() + ": " + id);
    }
    
    private void ConvertirAVenta (int id, int tipo, int margen) {
          FTPV tpv = new FTPV();
          this.getParent().add(tpv);
          Principal.centrarFrame(tpv);
          tpv.show();
          tpv.convertir(Compras.getListArticulos(id), tipo, margen);          
    }
    
    private void Convertir () {
        boolean primero = true;
        int idFac = -1;
        int pos = 0;
        if (tabla.isEditing())
            tabla.editingStopped(null);
        for (int i = 0; i < tabla.getRowCount(); i++) {
            if ((tabla.getValueAt(i, 9) != null) && ((Boolean) tabla.getValueAt(i, 9)).booleanValue()) {
                int id = Integer.parseInt("" + tabla.getValueAt(i, 0));
                if (primero) {      // primer albaran a pasar
                    primero = false;
                    ResultSet res = Compras.getCompra(id);
                    try {
                        if (!res.next()) {
                            return ;  
                        }
                        String datos [] = { new Fecha().getFechaSQL()+ " " + new Fecha().getHora(),
                           null,    // fechaCom
                           null,    // fechaPago 
                           null,    // fechaVenc
                           "",      // comentario
                           "",      // numFact
                           null,    // precio
                           null,    // sobrecargo
                           null,    // descuento
                           ""+res.getInt("idProve"), // codProve
                           "0",     // tipoticket
                           "1",     // tipoPago
                           "1"      // periodo
                           };        
                        idFac = Compras.guardar(datos, true);
                    } catch (SQLException ex) {
                        System.out.println("error en FFacturasCompras.convertir");
                    }
                }
                if (idFac == -1) {
                    log.error("FFacturasCompras.convertir no se genero la factura");
                    return;
                }
                    
                // borro albaran
                Compras.borrarCompra(id);
                //lo logueo para ver cuando se hizo
                log.info("Convertir " + cbTipo.getSelectedItem().toString() + ": " + id
                                    + " a Factura: " + idFac);   
            
                //inserto el numero de albaran en la lista
                String [] fecha =  tabla.getValueAt(i, 5).toString().substring(0,10).split("-");
                String datos2 [] = {"Albaran nº: " + id + "  Fecha: " + fecha[2] + "/" + fecha[1] + "/" + fecha[0], //detalle
                             "0", //descuento
                             null, //precio
                             null, //total
                             null, //costo
                             null, //cantidad
                             null, //codigo
                             null, //IGIC
                             ""+pos,  //pos 
                             ""+idFac
                           };
                Compras.guardarLista(datos2);
                
                // modifico los articulos
                String datos3[] = {""+idFac,""+(++pos), "" + id}; 
                pos = Compras.convertir(datos3);
            }  // if
        } // for
        if (idFac != -1)
            abrirCompra(idFac);
    }
    
    private void abrirCompra(int id) {
        FCompras comp = new FCompras();
        this.getParent().add(comp);
        Principal.centrarFrame(comp);
        comp.show();    
        comp.cargar(id);
    }
    
    private void HallarTotal() {
        float total = 0;
        for(int i = 0; i < tabla.getRowCount(); i++) {
            if (tabla.getValueAt(i, 2)!= null)
                total += Float.parseFloat(tabla.getValueAt(i, 2).toString());
        }
        txtTotal.setText(Util.getFormatPrecio(total,2));
    }
    
    private void HallarAPagar() {
        float total = 0;
        for(int i = 0; i < tabla.getRowCount(); i++) {
            if ((tabla.getValueAt(i, 2)!= null) && ((tabla.getValueAt(i, 8) == null) ||
                    (tabla.getValueAt(i, 8).toString().isEmpty()))) {
                float valor = Float.parseFloat(tabla.getValueAt(i, 2).toString()); 
                int periodo = Integer.parseInt(""+tabla.getValueAt(i, 7));
                total += valor / periodo;
            }
        }
        txtPagar.setText(Util.getFormatPrecio(total,2));
    }
 
    private void Colorear() {
        Color color = Util.getColorTicket(cbTipo.getSelectedItem().toString());
        cbTipoPago.setBackground(color);
        cbTipo.setBackground(color);
        txtAño.setBackground(color);
        txtDia.setBackground(color);
        txtMes.setBackground(color);
        txtAñoH.setBackground(color);
        txtDiaH.setBackground(color);
        txtMesH.setBackground(color);
        txtIdProve.setBackground(color);
        txtId.setBackground(color);
        txtIdFab.setBackground(color);
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        txtNumReg = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbTipo = new javax.swing.JComboBox();
        txtId = new javax.swing.JFormattedTextField();
        txtIdProve = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        cbTipoPago = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        txtDia = new javax.swing.JFormattedTextField();
        jLabel3 = new javax.swing.JLabel();
        txtMes = new javax.swing.JFormattedTextField();
        bBuscar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtAño = new javax.swing.JFormattedTextField();
        txtIdFab = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        cbFecha = new javax.swing.JComboBox();
        jLabel9 = new javax.swing.JLabel();
        txtDiaH = new javax.swing.JFormattedTextField();
        txtMesH = new javax.swing.JFormattedTextField();
        txtAñoH = new javax.swing.JFormattedTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        bBorrar = new javax.swing.JButton();
        bConvertirVenta = new javax.swing.JButton();
        bConvertir = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        txtPagar = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtSelec = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        cboxSelTodo = new javax.swing.JCheckBox();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gestion Compras");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/img/icono2.png"))); // NOI18N
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });
        getContentPane().setLayout(new java.awt.GridBagLayout());

        txtNumReg.setEditable(false);
        txtNumReg.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtNumReg.setMinimumSize(new java.awt.Dimension(80, 20));
        txtNumReg.setPreferredSize(new java.awt.Dimension(80, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 10);
        getContentPane().add(txtNumReg, gridBagConstraints);

        jLabel7.setText("Nº reg");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHEAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 95);
        getContentPane().add(jLabel7, gridBagConstraints);

        jScrollPane1.setMinimumSize(new java.awt.Dimension(720, 250));
        jScrollPane1.setPreferredSize(new java.awt.Dimension(720, 250));

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ID", "Proveedor", "Total", "Tipo", "Fecha", "Fecha", "periodod", "fecha", ""
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabla.setColumnSelectionAllowed(true);
        tabla.setMinimumSize(new java.awt.Dimension(45, 0));
        tabla.getTableHeader().setReorderingAllowed(false);
        tabla.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                tablaMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                tablaMouseExited(evt);
            }
        });
        tabla.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                tablaMouseMoved(evt);
            }
        });
        jScrollPane1.setViewportView(tabla);
        tabla.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        if (tabla.getColumnModel().getColumnCount() > 0) {
            tabla.getColumnModel().getColumn(0).setResizable(false);
            tabla.getColumnModel().getColumn(1).setResizable(false);
            tabla.getColumnModel().getColumn(2).setResizable(false);
            tabla.getColumnModel().getColumn(3).setResizable(false);
            tabla.getColumnModel().getColumn(4).setResizable(false);
            tabla.getColumnModel().getColumn(5).setResizable(false);
            tabla.getColumnModel().getColumn(6).setResizable(false);
            tabla.getColumnModel().getColumn(7).setResizable(false);
            tabla.getColumnModel().getColumn(8).setResizable(false);
        }

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(jScrollPane1, gridBagConstraints);

        jPanel1.setMinimumSize(new java.awt.Dimension(700, 70));
        jPanel1.setPreferredSize(new java.awt.Dimension(700, 70));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setText("Criterios de busqueda:   ");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 3;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 5, 0);
        jPanel1.add(jLabel1, gridBagConstraints);

        cbTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Factura", "Albaran", "Abono", "Todos" }));
        cbTipo.setMinimumSize(new java.awt.Dimension(80, 20));
        cbTipo.setPreferredSize(new java.awt.Dimension(80, 20));
        cbTipo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbTipoItemStateChanged(evt);
            }
        });
        cbTipo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                cbTipoKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        jPanel1.add(cbTipo, gridBagConstraints);

        txtId.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("00000000000000000000"))));
        txtId.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtId.setMinimumSize(new java.awt.Dimension(130, 20));
        txtId.setPreferredSize(new java.awt.Dimension(130, 20));
        txtId.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtIdMouseClicked(evt);
            }
        });
        txtId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtIdKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(txtId, gridBagConstraints);

        txtIdProve.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtIdProve.setMinimumSize(new java.awt.Dimension(100, 20));
        txtIdProve.setPreferredSize(new java.awt.Dimension(40, 20));
        txtIdProve.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtIdProveMouseClicked(evt);
            }
        });
        txtIdProve.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtIdProveKeyPressed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 0);
        jPanel1.add(txtIdProve, gridBagConstraints);

        jLabel6.setText("Id Proveedor");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 0, 5);
        jPanel1.add(jLabel6, gridBagConstraints);

        cbTipoPago.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Contado", "Credito" }));
        cbTipoPago.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbTipoPagoItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
        jPanel1.add(cbTipoPago, gridBagConstraints);

        jLabel2.setText("Dia");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 0, 0);
        jPanel1.add(jLabel2, gridBagConstraints);

        txtDia.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("00"))));
        txtDia.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDia.setToolTipText("dd");
        txtDia.setMinimumSize(new java.awt.Dimension(25, 20));
        txtDia.setPreferredSize(new java.awt.Dimension(25, 20));
        txtDia.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtDiaMouseClicked(evt);
            }
        });
        txtDia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDiaKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
        jPanel1.add(txtDia, gridBagConstraints);

        jLabel3.setText("Mes");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 9;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 0, 0);
        jPanel1.add(jLabel3, gridBagConstraints);

        txtMes.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("00"))));
        txtMes.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtMes.setToolTipText("mm");
        txtMes.setMinimumSize(new java.awt.Dimension(25, 20));
        txtMes.setPreferredSize(new java.awt.Dimension(25, 20));
        txtMes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtMesMouseClicked(evt);
            }
        });
        txtMes.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtMesKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 9;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
        jPanel1.add(txtMes, gridBagConstraints);

        bBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/buscar.png"))); // NOI18N
        bBuscar.setToolTipText("Buscar");
        bBuscar.setMinimumSize(new java.awt.Dimension(61, 61));
        bBuscar.setPreferredSize(new java.awt.Dimension(61, 61));
        bBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBuscarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 12;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 3;
        gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 0);
        jPanel1.add(bBuscar, gridBagConstraints);

        jLabel4.setText("Año");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 10;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.insets = new java.awt.Insets(5, 2, 0, 0);
        jPanel1.add(jLabel4, gridBagConstraints);

        txtAño.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("0000"))));
        txtAño.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtAño.setToolTipText("yyyy");
        txtAño.setMinimumSize(new java.awt.Dimension(25, 20));
        txtAño.setPreferredSize(new java.awt.Dimension(35, 20));
        txtAño.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtAñoMouseClicked(evt);
            }
        });
        txtAño.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtAñoKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 10;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
        jPanel1.add(txtAño, gridBagConstraints);

        txtIdFab.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtIdFab.setMinimumSize(new java.awt.Dimension(100, 20));
        txtIdFab.setPreferredSize(new java.awt.Dimension(100, 20));
        txtIdFab.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtIdFabMouseClicked(evt);
            }
        });
        txtIdFab.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtIdFabKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 3);
        jPanel1.add(txtIdFab, gridBagConstraints);

        jLabel8.setText("Id Fab");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 3);
        jPanel1.add(jLabel8, gridBagConstraints);

        cbFecha.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Compra", "Vencimiento" }));
        cbFecha.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbFechaItemStateChanged(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel1.add(cbFecha, gridBagConstraints);

        jLabel9.setText("Tipo de Fecha");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 0);
        jPanel1.add(jLabel9, gridBagConstraints);

        txtDiaH.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("00"))));
        txtDiaH.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtDiaH.setToolTipText("dd");
        txtDiaH.setMinimumSize(new java.awt.Dimension(25, 20));
        txtDiaH.setPreferredSize(new java.awt.Dimension(25, 20));
        txtDiaH.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtDiaHMouseClicked(evt);
            }
        });
        txtDiaH.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtDiaHKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 8;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
        jPanel1.add(txtDiaH, gridBagConstraints);

        txtMesH.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("00"))));
        txtMesH.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtMesH.setToolTipText("mm");
        txtMesH.setMinimumSize(new java.awt.Dimension(25, 20));
        txtMesH.setPreferredSize(new java.awt.Dimension(25, 20));
        txtMesH.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtMesHMouseClicked(evt);
            }
        });
        txtMesH.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtMesHKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 9;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
        jPanel1.add(txtMesH, gridBagConstraints);

        txtAñoH.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.NumberFormatter(new java.text.DecimalFormat("0000"))));
        txtAñoH.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtAñoH.setToolTipText("yyyy");
        txtAñoH.setMinimumSize(new java.awt.Dimension(25, 20));
        txtAñoH.setPreferredSize(new java.awt.Dimension(35, 20));
        txtAñoH.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtAñoHMouseClicked(evt);
            }
        });
        txtAñoH.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtAñoHKeyReleased(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 10;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
        jPanel1.add(txtAñoH, gridBagConstraints);

        jLabel11.setText("Desde");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel1.add(jLabel11, gridBagConstraints);

        jLabel12.setText("Hasta");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 7;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 0, 0);
        jPanel1.add(jLabel12, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
        gridBagConstraints.insets = new java.awt.Insets(11, 10, 0, 10);
        getContentPane().add(jPanel1, gridBagConstraints);

        jPanel2.setMinimumSize(new java.awt.Dimension(300, 80));
        jPanel2.setPreferredSize(new java.awt.Dimension(300, 60));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        bBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/eliminar.png"))); // NOI18N
        bBorrar.setToolTipText("Borra la linea seleccionada");
        bBorrar.setMinimumSize(new java.awt.Dimension(60, 60));
        bBorrar.setPreferredSize(new java.awt.Dimension(60, 60));
        bBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bBorrarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        jPanel2.add(bBorrar, gridBagConstraints);

        bConvertirVenta.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/fventas.png"))); // NOI18N
        bConvertirVenta.setToolTipText("La factura o albaran se utilizara para venta");
        bConvertirVenta.setMinimumSize(new java.awt.Dimension(60, 60));
        bConvertirVenta.setPreferredSize(new java.awt.Dimension(60, 60));
        bConvertirVenta.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bConvertirVentaActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        jPanel2.add(bConvertirVenta, gridBagConstraints);

        bConvertir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/img/factura.png"))); // NOI18N
        bConvertir.setToolTipText("<html><p align=center>El albaran o albaranes se convierte <br>\na factura eliminando los albaranes</p>\n\n");
        bConvertir.setMinimumSize(new java.awt.Dimension(60, 60));
        bConvertir.setPreferredSize(new java.awt.Dimension(60, 60));
        bConvertir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bConvertirActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 20, 0, 0);
        jPanel2.add(bConvertir, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 0);
        getContentPane().add(jPanel2, gridBagConstraints);

        jPanel3.setMinimumSize(new java.awt.Dimension(210, 80));
        jPanel3.setPreferredSize(new java.awt.Dimension(210, 90));
        jPanel3.setLayout(new java.awt.GridBagLayout());

        jLabel5.setText("Total");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        jPanel3.add(jLabel5, gridBagConstraints);

        txtTotal.setEditable(false);
        txtTotal.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtTotal.setMinimumSize(new java.awt.Dimension(100, 20));
        txtTotal.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 1;
        jPanel3.add(txtTotal, gridBagConstraints);

        txtPagar.setEditable(false);
        txtPagar.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtPagar.setMinimumSize(new java.awt.Dimension(100, 20));
        txtPagar.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(0, 2, 0, 0);
        jPanel3.add(txtPagar, gridBagConstraints);

        jLabel10.setText("A Pagar");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        jPanel3.add(jLabel10, gridBagConstraints);

        txtSelec.setEditable(false);
        txtSelec.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        txtSelec.setMinimumSize(new java.awt.Dimension(100, 20));
        txtSelec.setPreferredSize(new java.awt.Dimension(100, 20));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 2;
        jPanel3.add(txtSelec, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(0, 10, 10, 0);
        getContentPane().add(jPanel3, gridBagConstraints);

        jPanel4.setPreferredSize(new java.awt.Dimension(160, 60));
        jPanel4.setLayout(new java.awt.GridBagLayout());

        cboxSelTodo.setText("Seleccionar todo");
        cboxSelTodo.setToolTipText("");
        cboxSelTodo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboxSelTodoActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.SOUTH;
        jPanel4.add(cboxSelTodo, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.insets = new java.awt.Insets(0, 0, 10, 10);
        getContentPane().add(jPanel4, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 34) {
            int i = cbTipo.getSelectedIndex();
            if (i > cbTipo.getItemCount()) 
               i = 0;
            else
                i++;
            cbTipo.setSelectedIndex(i);
        }
    }//GEN-LAST:event_formKeyPressed

    private void cbTipoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_cbTipoKeyPressed
        if (evt.getKeyCode() == 10) 
            cbTipo.transferFocus();
    }//GEN-LAST:event_cbTipoKeyPressed

    private void tablaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseClicked
        int fila = tabla.rowAtPoint(evt.getPoint());
        int columna = tabla.columnAtPoint(evt.getPoint());
        if ((fila > -1) && (columna > -1)) {
            bBorrar.setEnabled(true);
            bConvertirVenta.setEnabled(true);
            //if (evt.getClickCount() == 2) {
            if (evt.isMetaDown()) {
                abrirCompra(Integer.parseInt(""+tabla.getValueAt(fila,0)));
            }
            if (columna == 9) {
               Boolean valor = false;
               float sum = 0;
               for (int i = 0; (i < tabla.getRowCount()); i++) {
                       if (!valor && Boolean.parseBoolean(""+tabla.getValueAt(i, 9))) {
                          valor = Boolean.parseBoolean(""+tabla.getValueAt(i, 9));
                       }   
                       if (Boolean.parseBoolean(""+tabla.getValueAt(i, 9)))
                            sum += Float.parseFloat(""+tabla.getValueAt(i, 2));
               }

               bConvertir.setEnabled(valor);
               txtSelec.setText(Util.getFormatPrecio(sum, 2));
            }
        }
        

    }//GEN-LAST:event_tablaMouseClicked

    private void txtIdProveKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdProveKeyPressed
        switch (evt.getKeyCode()) {
            case 113:       //f2
                FProveedores p = new FProveedores(this, Util.FFACCOMPRAS);
                this.getParent().add(p);
                Principal.centrarFrame(p);
                p.show();
                break;
            case 10:
                ActualizarTabla();
                break;
        }
    }//GEN-LAST:event_txtIdProveKeyPressed

    private void tablaMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseExited
        fuera = true;
    }//GEN-LAST:event_tablaMouseExited

    private void tablaMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseEntered
        fuera = false;
    }//GEN-LAST:event_tablaMouseEntered

    private void tablaMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaMouseMoved
        JTable aTable =  (JTable)evt.getSource();
        if (!fuera) {
            filaSel = aTable.rowAtPoint(evt.getPoint());
        }
    }//GEN-LAST:event_tablaMouseMoved

    private void bBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBuscarActionPerformed
        ActualizarTabla();
    }//GEN-LAST:event_bBuscarActionPerformed

    private void txtIdProveMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtIdProveMouseClicked
        txtIdProve.selectAll();
    }//GEN-LAST:event_txtIdProveMouseClicked

    private void txtIdMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtIdMouseClicked
        txtId.selectAll();
    }//GEN-LAST:event_txtIdMouseClicked

    private void txtDiaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtDiaMouseClicked
        txtDia.selectAll();
    }//GEN-LAST:event_txtDiaMouseClicked

    private void txtMesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtMesMouseClicked
        txtMes.selectAll();
    }//GEN-LAST:event_txtMesMouseClicked

    private void bBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bBorrarActionPerformed
        int fila = tabla.getSelectedRow();
        if (fila > -1) {
            String mensaje = "<html><p align=center> ¿Esta seguro de borrar?<br> PD: Asegurece de que no esta abierta la factura </p>";
            if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, mensaje,
                                            "Borrar", JOptionPane.OK_CANCEL_OPTION)) {
                Borrar(Integer.parseInt(""+tabla.getValueAt(fila, 0)));
                ActualizarTabla();
            }
        }
    }//GEN-LAST:event_bBorrarActionPerformed

    private void cbTipoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbTipoItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            ActualizarTabla();
            Colorear();
            txtSelec.setVisible(false);
            if (cbTipo.getSelectedIndex()==1) {
                txtSelec.setVisible(true);
            }
        }
    }//GEN-LAST:event_cbTipoItemStateChanged

    private void txtAñoMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtAñoMouseClicked
        txtAño.selectAll();
    }//GEN-LAST:event_txtAñoMouseClicked

    private void bConvertirVentaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bConvertirVentaActionPerformed
        int fila = tabla.getSelectedRow();
        if (fila > -1) {
            bConvertirVenta.setEnabled(true);
            int seleccion = JOptionPane.showOptionDialog(this, "Seleccione opcion", "En que se convertira", 
                                                        JOptionPane.YES_NO_CANCEL_OPTION,                    
                                                        JOptionPane.QUESTION_MESSAGE,
                                                        null,    // null para icono por defecto.
                                                        new Object[] { "Factura", "Albaran"},   // null para YES, NO y CANCEL
                                                        "Factura");
            if (seleccion != -1) {
                String marg = JOptionPane.showInputDialog(this, "Margen de beneficio");
                int margen = (marg == null) ? 0 : Integer.parseInt(marg);

                ConvertirAVenta(Integer.parseInt(""+tabla.getValueAt(fila, 0)), seleccion, margen);
                ActualizarTabla();
            }
        }
    }//GEN-LAST:event_bConvertirVentaActionPerformed

    private void txtDiaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDiaKeyReleased
        if (evt.getKeyCode() == 10) {
            txtMes.requestFocus();
        }
    }//GEN-LAST:event_txtDiaKeyReleased

    private void txtMesKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMesKeyReleased
        if (evt.getKeyCode() == 10) {
            txtAño.requestFocus();
        }
    }//GEN-LAST:event_txtMesKeyReleased

    private void txtAñoKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAñoKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
        }
    }//GEN-LAST:event_txtAñoKeyReleased

    private void cbTipoPagoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbTipoPagoItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED)
            ActualizarTabla();
    }//GEN-LAST:event_cbTipoPagoItemStateChanged

    private void txtIdKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
        }
    }//GEN-LAST:event_txtIdKeyReleased

    private void bConvertirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bConvertirActionPerformed
        if(JOptionPane.YES_OPTION == JOptionPane.showConfirmDialog(null, "¿Esta seguro? (los albaranes seran borrados)","Transformar a Factura", JOptionPane.OK_CANCEL_OPTION)) {
            Convertir();
            ActualizarTabla();
        }
    }//GEN-LAST:event_bConvertirActionPerformed

    private void cboxSelTodoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboxSelTodoActionPerformed
        if (tabla.isEditing()) 
            tabla.editingStopped(null);
        for (int i = 0; i < tabla.getRowCount(); i++)
               tabla.setValueAt(cboxSelTodo.isSelected(), i, 9);
            if (cbTipo.getSelectedIndex() == 1)
               bConvertir.setEnabled(cboxSelTodo.isSelected());
    }//GEN-LAST:event_cboxSelTodoActionPerformed

    private void txtIdFabMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtIdFabMouseClicked
        txtIdFab.selectAll();
    }//GEN-LAST:event_txtIdFabMouseClicked

    private void txtIdFabKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtIdFabKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
        }
    }//GEN-LAST:event_txtIdFabKeyReleased

    private void cbFechaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbFechaItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            boolean mostrar = false;
            if (cbFecha.getSelectedIndex() == 1) {
                mostrar = true;
            }
            txtPagar.setVisible(mostrar);
            jLabel10.setVisible(mostrar);
            txtDia.setEnabled(!mostrar);
        }
    }//GEN-LAST:event_cbFechaItemStateChanged

    private void txtDiaHMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtDiaHMouseClicked
        txtDiaH.selectAll();
    }//GEN-LAST:event_txtDiaHMouseClicked

    private void txtDiaHKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtDiaHKeyReleased
        if (evt.getKeyCode() == 10) {
            txtMesH.requestFocus();
        }
    }//GEN-LAST:event_txtDiaHKeyReleased

    private void txtMesHMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtMesHMouseClicked
        txtMesH.selectAll();
    }//GEN-LAST:event_txtMesHMouseClicked

    private void txtMesHKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMesHKeyReleased
        if (evt.getKeyCode() == 10) {
            txtAñoH.requestFocus();
        }
    }//GEN-LAST:event_txtMesHKeyReleased

    private void txtAñoHMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtAñoHMouseClicked
        txtAñoH.selectAll();
    }//GEN-LAST:event_txtAñoHMouseClicked

    private void txtAñoHKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtAñoHKeyReleased
        if (evt.getKeyCode() == 10) {
            ActualizarTabla();
        }
    }//GEN-LAST:event_txtAñoHKeyReleased

         
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bBorrar;
    private javax.swing.JButton bBuscar;
    private javax.swing.JButton bConvertir;
    private javax.swing.JButton bConvertirVenta;
    private javax.swing.JComboBox cbFecha;
    private javax.swing.JComboBox cbTipo;
    private javax.swing.JComboBox cbTipoPago;
    private javax.swing.JCheckBox cboxSelTodo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabla;
    private javax.swing.JFormattedTextField txtAño;
    private javax.swing.JFormattedTextField txtAñoH;
    private javax.swing.JFormattedTextField txtDia;
    private javax.swing.JFormattedTextField txtDiaH;
    private javax.swing.JFormattedTextField txtId;
    private javax.swing.JTextField txtIdFab;
    public javax.swing.JTextField txtIdProve;
    private javax.swing.JFormattedTextField txtMes;
    private javax.swing.JFormattedTextField txtMesH;
    private javax.swing.JTextField txtNumReg;
    private javax.swing.JTextField txtPagar;
    private javax.swing.JTextField txtSelec;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables
}
